import CONFIG from './config.js'

class GameEvent {
    constructor(game) {
        this.game = game;
        this.mouse_object = game.weapon_utils.new_weapon(1);
        this.handle = document.getElementById(CONFIG.operation_id);

        this.add_handle();
        this.start_btn = document.getElementById("start");
        this.empty_btn = document.getElementById("empty");
        this.reg_event();
    }

    add_handle() {
        let g_event = this;
        Array.apply(null, g_event.handle.children).forEach(function (item) {
            item.remove();
        });

        CONFIG.weapon_type.forEach(function (config, index) {
            let ele = document.createElement("div");
            ele.id = config.name;
            ele.type = config.type;
            ele.innerHTML = "<div>" + config.label + "</div>" +
                "<div class='tip'>" + (config.gold ? config.gold : "") + "</div>";
            g_event.handle.appendChild(ele);
        });
        g_event.handle.children[1].className = "select";

        let op_arr = [
            {action: "separator", label: ""},
            {action: "start", label: "开始"},
            {action: "restart", label: "重启"}
        ];
        op_arr.forEach(function (config, index) {
            let ele = document.createElement("div");
            ele.id = config.action;
            ele.innerText = config.label;
            g_event.handle.appendChild(ele);
        });
    }

    reset_handle() {
        this.start_btn.innerText = "开始";
    };

    draw() {
        let game = this.game;
        let mouse_object = this.mouse_object;
        if (!mouse_object) return;

        let object_type = this.mouse_object.config.type;
        if (object_type === 1) {
            let row = mouse_object.row;
            let col = mouse_object.col;
            let select_weapon = game.weapon_utils.find_weapon(row, col);
            select_weapon && select_weapon.show_fire_range();
        }
        mouse_object.draw && mouse_object.draw(true);
    };

    reg_event() {
        let canvas_stage = document.getElementById(CONFIG.canvas_stage_id);

        let g_event = this;
        let game = this.game;
        let map = this.game.map;

        let canvas_stage_onmousemove = function (event) {
            let width = event.offsetX * map.map_info.scale;
            let height = event.offsetY * map.map_info.scale;
            let col = (width / map.map_info.cell_width) | 0;
            let row = (height / map.map_info.cell_height) | 0;
            g_event.mouse_object && g_event.mouse_object.set_position(row, col);
        };

        let canvas_stage_onclick = function (event) {
            let is_touch = false;
            let pos_x = event.offsetX * map.map_info.scale;
            let pos_y = event.offsetY * map.map_info.scale;
            if (event.type === "touchstart") {
                is_touch = true;
                let target = event.touches[0];
                let parent = event.target.parentElement;
                let x = target.pageX - parent.offsetLeft;
                let y = target.pageY - parent.offsetTop;
                pos_x = x * map.map_info.scale;
                pos_y = y * map.map_info.scale;
            }

            let map_cell = map.xy_2_vector(pos_x, pos_y);
            if (!map_cell) return;

            let row = map_cell.row;
            let col = map_cell.col;
            g_event.mouse_object && g_event.mouse_object.set_position(row, col);

            let object_type = g_event.mouse_object.config.type;
            if (object_type > 1) {
                let success = game.weapon_utils.add_weapon(object_type, row, col);
                if (object_type > 2 && success) {
                    let event_type = is_touch ? "touchstart" : "click";
                    let evt = document.createEvent('Event');
                    evt.initEvent(event_type, true, true);
                    g_event.empty_btn.dispatchEvent(evt);

                    g_event.mouse_object = game.weapon_utils.new_weapon(1);
                }
            }
            if (object_type === 0) {
                game.weapon_utils.remove_weapon(row, col);
            }
        };

        let canvas_stage_onmouseout = function () {
            g_event.mouse_object && g_event.mouse_object.set_position(-1, -1);
        };

        let g_event_handle_onclick = function (event) {
            let target = event.currentTarget;

            if (target.type !== undefined) {
                let handle_list = Array.apply(null, g_event.handle.children);
                handle_list.forEach(function (ele) {
                    ele.className = ""
                });

                target.className = "select";
                let weapon_index = parseInt(target.type);
                g_event.mouse_object = game.weapon_utils.new_weapon(weapon_index);

            } else {
                if (target.id === "start") {
                    switch (game.game_info.status) {
                        case "stop":
                            game.start();
                            target.innerText = "暂停";
                            break;
                        case "running":
                            game.game_info.status = "pause";
                            target.innerText = "继续";
                            let msg = {text: "暂停中", size: 6, duration: 0};
                            Message.add_message(msg, true);
                            Message.show_msg(game);
                            break;
                        case "pause":
                            target.innerText = "暂停";
                            game.game_info.status = "running";
                            Message.clear_message();
                            break;
                    }
                }
                if (target.id === "restart") {
                    g_event.start_btn.innerText = "暂停";
                    if (confirm("确认重启游戏？")) game.start();
                }
            }
        };

        let document_onresize = function () {
            map.init_map_info();
        };

        window.addEventListener("resize", document_onresize);
        canvas_stage.addEventListener("mouseout", canvas_stage_onmouseout);
        canvas_stage.addEventListener("mousemove", canvas_stage_onmousemove);

        canvas_stage.addEventListener("click", canvas_stage_onclick);
        Array.apply(null, g_event.handle.children).forEach(function (item) {
            item.addEventListener("click", g_event_handle_onclick);
        });

        g_event.start_btn.addEventListener("touchstart", function (event) {
            if (!GameEvent.touch_mode) {
                canvas_stage.removeEventListener("click", canvas_stage_onclick);
                canvas_stage.addEventListener("touchstart", canvas_stage_onclick);
                Array.apply(null, g_event.handle.children).forEach(function (item) {
                    item.removeEventListener("click", g_event_handle_onclick);
                    item.addEventListener("touchstart", g_event_handle_onclick);
                });
                GameEvent.touch_mode = true;
                g_event_handle_onclick(event);
            }
        });
    }
}

export default GameEvent;